﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IWMAG.Core.Objects {
    public class AnimationController {
        protected int Animation = 0;
        protected int CurrentFrame = 0;
        protected float Duration = 0;
        protected AnimationSet Set;

        public void Update(float elapsed) {
            Duration += elapsed;

            Frame f = Set.Animations[Animation][CurrentFrame];

            if (Duration >= f.Duration) {
                Duration = 0;
                CurrentFrame++;
                if (f.Script != null)
                    f.Script.RaiseEvent(EventType.OnNewFrame, null, new object[] {});

                if (CurrentFrame >= Set.Animations[Animation].Count)
                    Play(0);
            }
        }

        protected void Play(int anim) {
            Animation = anim;
            Duration = 0;
            CurrentFrame = 0;
        }

        public int GetFrame() {
            return Set.Animations[Animation][CurrentFrame].FrameId;
        }
    }
}
